//
//  OpenLidLayer.cpp
//  slushy
//
//  Created by luotianqiang1 on 17/5/26.
//
//

#include "OpenLidLayer.h"
#include "FrameStatus.h"
#include "TouchNodeComponent.h"
#include "particleLayer.h"
#include "MakeIce.h"
IMPLEMENT_COCOS2DX_CLASS(OpenLidLayer);
REGIST_SECENE(OpenLidLayer)
bool OpenLidLayer::init(){
    if(BaseStepLayer::init()) {
        _operate = StudioLayer::create("OpenLidLayer.csb");
        addChild(_operate);
        _prompt = _operate->getNodeByName<Sprite*>("finger");
        vector<string> hideNodes = {"cola","tool"};
        for(auto name:hideNodes)
            _operate->getNodeByName(name)->setVisible(false);
        openCount =0;
        SimpleAudioEngine::getInstance()->preloadEffect("sound/Bottle open.mp3");
        return true;
    }
    return false;
}

void OpenLidLayer::touchEnd(ui::Widget* widget){
    
}


void OpenLidLayer::onEnterTransitionDidFinish(){
    BaseStepLayer::onEnterTransitionDidFinish();
    auto cola = _operate->getNodeByName("cola");
    ActionHelper::showBackInOut(cola, cola->getPosition(), ActionHelper::ShowDirection::show_from_left,nullptr,0.9f);
    cola->setVisible(true);
    
    ActionHelper::delayFunc(0.3f, this, [=](){
        auto tool = _operate->getNodeByName("tool");
        ActionHelper::showBackInOut(tool, tool->getPosition(), ActionHelper::ShowDirection::show_from_right,[=](){
            auto touchMove = MoveHideShade::create();
            touchMove->setTarget(LQRect::create(CocosHelper::getNodeRectInWorld(_operate->getNodeByName("limit"))));
            touchMove->setMoveType(MoveComponentType::kTypeBack);
            tool->addComponent(touchMove);
            ActionHelper::runBezierMoveHideAction(_prompt, tool->convertToWorldSpace(tool->getContentSize()*.5), _operate->getPosinWord("lid"));
            touchMove->addListners(ComponentTouchMove, [=](LQComponent*,OperateListner*){
                if(touchMove->checkInlimit()){
                    this->stopPrompt();
                    touchMove->setEnabled(false);
                    vector<Node*> moveNodes = {cola,tool};
                    for(auto moveNode:moveNodes){
                        auto f = FrameStatus::create(moveNode->getName());
                        f->setSearchAction(_operate->getAction());
                        moveNode->runAction(Sequence::create(f->createAction(0.7f, "moveEnd"),CallFunc::create([=](){
                            if(moveNode == tool){
                                tool->removeComponent(touchMove);
                                moveInTarget();
                            }
                        }), nullptr));
                        f->remove();
                    }
                }
            });
        },0.9f);
        tool->setVisible(true);
    });
}

void OpenLidLayer::openLid(LQComponent* lc,OperateListner* lis){
    auto tool = lc->getOwner();
    auto size = tool->getContentSize();
    auto touchPos = dynAny_cast<Vec2>(lis->getData());
    auto ritghtPos = tool->convertToWorldSpace(Vec2(size.width,size.height*tool->getAnchorPoint().y));
    auto centerPos = tool->convertToWorldSpace(Vec2(size.width,size.height*tool->getAnchorPoint().y));
    auto endDirect = touchPos - centerPos;
    auto angel = endDirect.getAngle(Vec2(0,-1));
    auto endRotate = angel*180/3.1415926f;
    if((int)endRotate%360>rotatetion)
        endRotate = rotatetion;
    else if(endRotate<rotatetion-3){
        endRotate = rotatetion-3;
        this->stopPrompt();
        _prompt->setRotation(0);
        lc->setEnabled(false);
        tool->setRotation(rotatetion);
        auto lid = _operate->getNodeByName("lid");
        lid->runAction(Sequence::create(DelayTime::create(1.5f),RotateTo::create(0.15f, lid->getRotation()-3), nullptr));
        tool->runAction(MoveBy::create(2, Vec2(-0.7,1)));
        tool->runAction(Sequence::create(Repeat::create(Sequence::create(RotateBy::create(0.5, 10),RotateBy::create(0.5, -10), nullptr), 2), CallFunc::create([=](){
            openCount++;
            if(openCount == 3){
                SoundPlayer::getInstance()->playEffect("sound/Bottle open.mp3");
                tool->runAction(RotateBy::create(0.5, 10));
                lid->runAction(RotateBy::create(1.7, 1200));
                lid->runAction(Sequence::create(EaseExponentialOut::create(MoveBy::create(1.7, Vec2(-300,300))),CallFunc::create([=](){
                    lid->removeFromParent();
                }), nullptr));
                
                ActionHelper::delayFunc(0.75f, this, [=](){
                    ActionHelper::hide(tool, ActionHelper::ShowDirection::show_from_right);
                    cutAllEnd();
                });
            }else {
                lc->setEnabled(true);
                rotatetion = tool->getRotation();
                auto rightPos = tool->convertToWorldSpace(Vec2(tool->getContentSize().width-10,tool->getContentSize().height*.5f));
                ActionHelper::runMoveHideAction(_prompt, rightPos, rightPos+Vec2(-30,85));
            }
        }), nullptr));
        
    }else {
        tool->setRotation(endRotate);
    }
}

void OpenLidLayer::moveInTarget(){
    auto tool = _operate->getNodeByName("tool");
    auto toolLimit = _operate->getNodeByName("toolLimit");
    rotatetion = toolLimit->getRotation();
    CocosHelper::reAddNode(tool, toolLimit);
    tool->setRotation(tool->getRotation()-toolLimit->getRotation());
    
    auto rightPos = toolLimit->convertToWorldSpace(Vec2(toolLimit->getContentSize().width-10,toolLimit->getContentSize().height*.5f));
    ActionHelper::runMoveHideAction(_prompt, rightPos, rightPos+Vec2(-30,85));
    auto openComponent = TouchNodeComponent::create();
    openComponent->addListners(ComponentTouchMove, CC_CALLBACK_2(OpenLidLayer::openLid, this));
    auto touchEnd = [=](LQComponent*,OperateListner* lis){
        toolLimit->stopAllActions();
        toolLimit->runAction(EaseExponentialIn::create(RotateTo::create(0.4f, rotatetion)));
    };
    openComponent->addListners(ComponentTouchEnd, touchEnd);
    openComponent->addListners(ComponentTouchCancle, touchEnd);
    toolLimit->addComponent(openComponent);
}

void OpenLidLayer::cutAllEnd(){
    auto bowl = _operate->getNodeByName("cola");
    bowl->runAction(Sequence::create(ScaleTo::create(0.9f, 1.25f),CallFunc::create([=](){
        this->addChild(particleLayer::create(),10);
    }),DelayTime::create(1.5),CallFunc::create([](){
        Director::getInstance()->replaceScene(TransitionPageTurn::create(1.2, MakeIce::createScene(), false));
    }), nullptr));
    bowl->runAction(JumpTo::create(0.9f, bowl->getParent()->getContentSize()*.5f, 200, 1));
}